#include "constantimport.h"

#include <QFile>
#include <QtXml>
#include <QDebug>
#include <QDomElement>
#include <QDomDocument>
static QMap<QString, int> constantMap;

ConstantImport::ConstantImport(QObject *parent) : QObject(parent)
{
    hash["int8"]="int";
    hash["int16"]="short";
    hash["int32"]="int";
    hash["int64"]="int";
    hash["float32"]="float";
    hash["float64"]="float";
    hash["uint8"]="uint";
    hash["uint16"]="ushort";
    hash["uint32"]="uint";
    hash["uint64"]="uint";

    suffix["int16"]="s";
    suffix["float32"]="f";
    suffix["float64"]="f";
    suffix["uint8"]="u";
    suffix["uint16"]="us";
    suffix["uint32"]="u";
    suffix["uint64"]="u";
}

void ConstantImport::clearConstantMap()
{
    constantMap.clear();
}


bool ConstantImport::constantImport_64(const QString & filePath,const QDomNode &node)
{
    if (node.toElement().tagName() != "File"&&node.toElement().tagName() != "Package")
        return false;

    QDomElement element = node.firstChild().toElement();

    if(element.tagName() != "declarations")
    {
        return false;
    }

    QDomNodeList items = element.childNodes();

    //QList<QDomNode> constList;

    for (int i = 0;i < items.count();i++)
    {
        element = items.at(i).toElement();

        if(element.isNull() || element.tagName() != "Constant" || !element.hasAttribute("name"))
            continue;

        ConstantDefine::Constant constant;
        constant.node=element;
        parseConstants(constant,constant.node.toElement());
        //constList.append(element);//添加所有常量节点
        m_datas.append(constant);

    }

    QFile file(filePath);

    //默认模型文件不存在时
    if (!file.open(QFile::ReadOnly| QFile::Text))
    {
        if (!file.open(QFile::WriteOnly| QFile::Text|QFile::Truncate))
        {
            return false;
        }

        QDomDocument doc;

        QDomProcessingInstruction instruction;

        instruction = doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");
        doc.appendChild(instruction);

        QDomElement root = doc.documentElement();
        if(root.isNull())
        {
            root = doc.createElement("File");
            root.setAttribute("Type","comm");

            QDomElement declarations = doc.createElement("declarations");

            for (int i = 0;i < m_datas.size();i++)
            {
                QDomNode constNode=m_datas[i].node;
                QString constName = constNode.toElement().attribute("name");
                if(constantMap.find(constName) != constantMap.end()){
                    continue;
                }
                constantMap.insert(constName, 1);

                constListProcess_64(constNode,doc);

                declarations.appendChild(constNode);
            }

            root.appendChild(declarations);
        }

        doc.appendChild(root);

        QTextStream stream( &file );
        stream.setCodec("utf-8");
        doc.save(stream,2,QDomNode::EncodingFromTextStream);

        file.close();
    }
    else {

        QDomDocument doc;

        if(!doc.setContent(&file))
        {
            file.close();
            return false;
        }

        file.close();

        QDomElement root = doc.documentElement();
        if(root.tagName()!="File")
        {
            return false;
        }

        QDomElement declarations = root.firstChild().toElement();
        if(declarations.tagName()!="declarations")
            return false;

        for (int i = 0;i < m_datas.size();i++)
        {
            QDomNode constNode=m_datas[i].node;
            QString constName = constNode.toElement().attribute("name");
            if(constantMap.find(constName) != constantMap.end()){
                continue;
            }
            constantMap.insert(constName, 1);
            constListProcess_64(constNode,doc);

            declarations.appendChild(constNode);
        }


        if(file.open(QFile::WriteOnly|QFile::Truncate))
        {
            QTextStream stream( &file );
            stream.setCodec("utf-8");
            doc.save(stream,2,QDomNode::EncodingFromTextStream);
        }

        file.close();
    }

    return true;
}

void ConstantImport::constListProcess_64(QDomNode &constNode,QDomDocument &doc)
{
    QDomNode typeNode=constNode.firstChild();

    QDomNode valueNode=typeNode.nextSibling();

    QDomNode pragmasNode=valueNode.nextSibling();

    QDomNode idNode=pragmasNode.firstChild();

    QDomNode commentNode=idNode.firstChild();

    QString oid;//oid
    QString comment;//注释

    oid=QUuid::createUuid().toString(QUuid::WithoutBraces);

    if(!commentNode.isNull())
        comment=commentNode.toElement().text();

    constNode.removeChild(pragmasNode);

    QDomElement oidEle=doc.createElement("oid");
    oidEle.setAttribute("value",oid);
    constNode.appendChild(oidEle);

    if(!comment.isEmpty())
    {
        QDomElement pragmasEle=doc.createElement("pragmas");
        constNode.appendChild(pragmasEle);

        QDomElement commentEle=doc.createElement("comment");
        commentEle.setAttribute("value",comment);
        pragmasEle.appendChild(commentEle);
    }
}

bool ConstantImport::readConstantFile(const QDomNode &root)
{
    // 读取文件
    m_datas.clear();

    if (root.toElement().tagName() != "File"&&root.toElement().tagName() != "Package")
        return false;

    QDomElement element=root.firstChild().toElement();

    // declarations节点不存在
    if(element.tagName() != "declarations")
    {
        return false;
    }

    // 获取所有常量节点，解析常量数据
    QDomNodeList items = element.childNodes();
    for (int i = 0;i < items.count();i++)
    {
        QDomNode item = items.at(i);
        element = item.toElement();
        //parseConstants(element);
    }

    return true;
}

void ConstantImport::dataProcess(QFile &file,QStringList &deleteTypes,ConstantDefine::ConstantList &datas)
{
    QHash<QString,QString> hash;
    hash["int8"]="int";
    hash["int16"]="short";
    hash["int32"]="int";
    hash["int64"]="int";
    hash["float32"]="float";
    hash["float64"]="float";
    hash["uint8"]="uint";
    hash["uint16"]="ushort";
    hash["uint32"]="uint";
    hash["uint64"]="uint";

    QList<int> dataRemoveIndex;

    for (int i = 0;i < datas.size();i++)
    {
        if(deleteTypes.contains(datas.at(i).typeName))
        {
            if( !file.open(QIODevice::Append | QIODevice::Text) )
                return;

            QTextStream out(&file);

            out<<datas.at(i).name+QString("因数据类型无法导入")<<endl;
            file.close();
            dataRemoveIndex.append(i);
//            datas.removeAt(i);//移除不支持的常量
        }
        else
        {
            typeConvert(hash,datas[i]);//数据类型转换
            oidConvert(datas[i]);//oid转换
        }


    }

    for(int i=dataRemoveIndex.size()-1;i>=0;i--)
    {
        datas.removeAt(dataRemoveIndex.at(i));
    }
}

bool ConstantImport::saveConstantFile(const QString & filePath, const ConstantDefine::ConstantList &datas)
{

    QFile file(filePath);
    if (!file.open(QFile::ReadOnly| QFile::Text))
    {
        if (!file.open(QFile::WriteOnly| QFile::Text|QFile::Truncate))
        {
            return false;
        }

        QDomDocument doc;

        QDomProcessingInstruction instruction;

        instruction = doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");
        doc.appendChild(instruction);

        QDomElement root = doc.documentElement();
        if(root.isNull())
        {
            root = doc.createElement("File");
            root.setAttribute("Type","comm");

            QDomElement declarations = doc.createElement("declarations");

            for (int i = 0;i < datas.size();i++)
            {
                saveConstants(declarations,datas.at(i),doc);
            }

            root.appendChild(declarations);
        }

        doc.appendChild(root);

        QTextStream stream( &file );
        stream.setCodec("utf-8");
        doc.save(stream,2,QDomNode::EncodingFromTextStream);

        file.close();
    }
    else {
        QDomDocument doc;

        if(!doc.setContent(&file))
        {
            file.close();
            return false;
        }

        file.close();

        QDomElement root = doc.documentElement();
        if(root.tagName()!="File")
        {
            return false;
        }

        QDomElement declarations = root.firstChild().toElement();
        if(declarations.tagName()!="declarations")
            return false;

        for (int i = 0;i < datas.size();i++)
        {
            saveConstants(declarations,datas.at(i),doc);
        }

        if(file.open(QFile::WriteOnly|QFile::Truncate))
        {
            QTextStream stream( &file );
            stream.setCodec("utf-8");
            doc.save(stream,2,QDomNode::EncodingFromTextStream);
        }

        file.close();
    }

    return true;
}

void ConstantImport::parseConstants(ConstantDefine::Constant &constant,const QDomElement &element)
{
    // 判断节点是否存在
    if(element.isNull() || element.tagName() != "Constant" || !element.hasAttribute("name"))
        return;

    QString constantName = element.attribute("name");

    QDomNode typeNode;
    QDomNode valueNode;
    QDomNode idNode;
    QDomNode paragmasNode;
    QDomNode commentNode;

    QString oid;
    QString comment;

    // 判断是否存在子节点
    if(!element.hasChildNodes())
        return;

    QDomNodeList childNodeList = element.childNodes();

    typeNode = element.firstChild();
    valueNode = typeNode.nextSibling();
    paragmasNode = valueNode.nextSibling();
    idNode = paragmasNode.firstChild();
    if(!idNode.firstChild().isNull())
        commentNode = idNode.firstChild();

    oid = getOid(idNode);
    comment = getComment(commentNode);

    // 判断是否存在子节点
    if(!typeNode.hasChildNodes() || !valueNode.hasChildNodes())
        return;

    QDomElement typeElement = typeNode.toElement();
    QDomElement valueElement = valueNode.toElement();

    parseNormalData(constant,typeElement,valueElement,constantName,oid,comment);

}

void ConstantImport::parseNormalData(ConstantDefine::Constant &data,const QDomElement &typeElement, const QDomElement &valueElement, const QString &constantName, const QString &oid, const QString &comment)
{
    //ConstantDefine::Constant data;
    QString typeName;
    if(typeElement.firstChildElement().tagName()=="NamedType")
    {
        QDomNode namedNode = typeElement.firstChild();

        if(!namedNode.hasChildNodes() || !namedNode.firstChild().hasChildNodes() || !valueElement.hasChildNodes())
            return;

        QDomNode typeRefNode = namedNode.firstChild().firstChild();
        QDomElement typeRefElement = typeRefNode.toElement();
        QString typeRefTagName = typeRefElement.tagName();
        if(typeRefTagName == "TypeRef" && typeRefElement.hasAttribute("name"))
        {
            typeName = typeRefElement.attribute("name");
            if(hash.contains(typeName))
            {
                typeRefElement.setAttribute("name",hash.value(typeName));
            }
        }

    }
    else if(typeElement.firstChildElement().tagName()=="Table")//适配一维匿名数组
    {
        QDomNode namedNode = typeElement.firstChild().firstChild().firstChild();

        if(!namedNode.hasChildNodes() || !namedNode.firstChild().hasChildNodes() || !valueElement.hasChildNodes())
            return;

        QDomNode typeRefNode = namedNode.firstChild().firstChild();
        QDomElement typeRefElement = typeRefNode.toElement();
        QString typeRefTagName = typeRefElement.tagName();
        if(typeRefTagName == "TypeRef" && typeRefElement.hasAttribute("name"))
        {
            typeName = typeRefElement.attribute("name");
            if(hash.contains(typeName))
            {
                typeRefElement.setAttribute("name",hash.value(typeName));
            }
        }
        data.array.arrayType=typeName;
    }

    QDomNode valueChildNode = valueElement.firstChild();
    QDomElement valueChildElement = valueChildNode.toElement();
    QString valueTagName = valueChildElement.tagName();

    data.name = constantName;
    data.typeName = typeName;
    data.comments = comment;
    data.oid = oid;



    // 解析普通数据
    if(valueTagName == "ConstValue")
    {
        parseConstValue(valueChildElement,data);
    }

    // 解析结构体数据
    if(valueChildNode.nextSibling().toElement().tagName()=="DataStructOp")
    {
        parseDataStructOp(valueChildNode.nextSibling().toElement(),data);
    }

    // 解析数组数据
    if(valueChildNode.nextSibling().toElement().tagName()=="DataArrayOp")
    {
        parseDataArrayOp(valueChildNode.nextSibling().toElement(),data);
    }



    //m_datas.append(data);
}

void ConstantImport::parseConstValue(QDomElement &constValueElement, ConstantDefine::Constant &data)
{
    // 解析普通数据
    if(constValueElement.hasAttribute("value"))
    {
        QString value = constValueElement.attribute("value");
        if(suffix.contains(data.typeName))
        {
            constValueElement.setAttribute("value",value+suffix.value(data.typeName));
        }
        data.value = value;

    }
}

void ConstantImport::parseDataStructOp(const QDomElement &dataStructOpElement, ConstantDefine::Constant &data)
{
    // 解析结构体数据
    data.baseType = "<structure>";

    // 不存在子节点返回
    if(!dataStructOpElement.hasChildNodes())
        return;

    ConstantDefine::StructEleList structEleList;
    parseDataStructOp(dataStructOpElement,structEleList);
    data.structElements = structEleList;
}

void ConstantImport::parseDataArrayOp(const QDomElement &dataArrayOpElement, ConstantDefine::Constant &data)
{
    // 解析数组数据
    data.baseType = "array";

    if(!dataArrayOpElement.hasChildNodes())
        return;

    if(dataArrayOpElement.firstChild().toElement().tagName() != "data")
        return;

    ConstantDefine::DimensionArray array;
    array.arrayType=data.array.arrayType;
    QDomElement dataElement = dataArrayOpElement.firstChild().toElement();
    QDomElement nodeElement = dataElement.firstChild().toElement();
    QDomNodeList valueList = dataElement.childNodes();

    QString nodeName = nodeElement.tagName();

    // 节点名为普通节点
    if(nodeName == "ConstValue")
    {
        if(nodeElement.hasAttribute("value"))
        {
            QString value = nodeElement.attribute("value");

        }
        for(int i = 0; i < valueList.size(); ++i)
        {
            QDomNode value = valueList.at(i);
            QDomElement valueElement = value.toElement();
            parseConstValue(valueElement,array);
        }
    }


    // 节点名为数组或结构体
    if(dataElement.firstChild().nextSibling().toElement().tagName() == "DataStructOp" || dataElement.firstChild().nextSibling().toElement().tagName() == "DataArrayOp")
    {
        if(dataElement.firstChild().nextSibling().toElement().tagName() == "DataStructOp")
            array.arrayType = "<structure>";

        if(dataElement.firstChild().nextSibling().toElement().tagName() == "DataArrayOp")
            array.arrayType = "array";

        for(int i = 0; i < valueList.size(); ++i)
        {
            QDomNode value = valueList.at(i);
            QDomElement valueElement = value.toElement();
            if(valueElement.tagName()=="DataStructOp"||valueElement.tagName()=="DataArrayOp")
                parseDataArray(valueElement,array);
        }
    }

    data.array = array;
}

void ConstantImport::parseDataStructOp(const QDomElement &dataStructOpElement, ConstantDefine::StructEleList &datas)
{
    // 解析结构体列表
    QDomElement dataElement = dataStructOpElement.firstChild().toElement();
    QDomNodeList labelList = dataElement.childNodes();

    for(int i = 0; i < labelList.size(); ++i)
    {
        ConstantDefine::StructEle structEle;

        QDomNode label = labelList.at(i);
        QDomElement labelElement = label.toElement();

        // 是否存在结构图label
        if(labelElement.tagName() == "LabelledExpression" && labelElement.hasAttribute("label"))
        {
            structEle.elementName = labelElement.attribute("label");
            QDomElement flowElement = labelElement.firstChild().toElement();
            QDomNode flowChildNode = flowElement.firstChild();
            QDomElement flowChildElement = flowChildNode.toElement();
            QString flowChildTagName = flowChildElement.tagName();

            // label元素为普通类型
            if(flowChildTagName == "ConstValue")
            {
                parseConstValue(flowChildElement,structEle);
            }

            // label元素为数组或结构体
            if(flowChildNode.nextSibling().toElement().tagName() == "DataStructOp" || flowChildNode.nextSibling().toElement().tagName() == "DataArrayOp")
            {
                parseDataStruct(flowChildNode.nextSibling().toElement(),structEle);
            }
        }
        datas.append(structEle);
    }
}

void ConstantImport::parseConstValue(const QDomElement &flowChildElement, ConstantDefine::StructEle &data)
{
    // 解析结构体元素
    if(flowChildElement.hasAttribute("value"))
    {
        QString value = flowChildElement.attribute("value");
//        if(suffix.contains(data.))
//        {
//            constValueElement.setAttribute("value",value+suffix.value(data.typeName));
//        }
        data.elementValue = value;

    }
}

void ConstantImport::parseDataStruct(const QDomElement &flowChildElement, ConstantDefine::StructEle &data)
{
    // 解析结构体元素
    QString flowChildTagName = flowChildElement.tagName();

    // 普通元素
    if(flowChildTagName == "ConstValue")
    {
        parseConstValue(flowChildElement,data);
    }

    // 结构体
    if(flowChildTagName == "DataStructOp")
    {
        ConstantDefine::StructEleList structEleList;
        data.elementDef="<structure>";
        parseDataStructOp(flowChildElement,structEleList);
        data.structEles = structEleList;
    }

    // 数组
    if(flowChildTagName == "DataArrayOp")
    {
        ConstantDefine::DimensionArray array;
        data.elementDef="array";

        QDomElement dataElement = flowChildElement.firstChild().toElement();
        QDomElement nodeElement = dataElement.firstChild().toElement();
        QDomNodeList valueList = dataElement.childNodes();

        QString nodeName = nodeElement.tagName();

        // 节点名为普通节点
        if(nodeName == "ConstValue")
        {
            if(nodeElement.hasAttribute("value"))
            {
                QString value = nodeElement.attribute("value");

            }
            for(int i = 0; i < valueList.size(); ++i)
            {
                QDomNode value = valueList.at(i);
                QDomElement valueElement = value.toElement();
                parseConstValue(valueElement,array);
            }
        }


        // 节点名为数组或结构体
        if(dataElement.firstChild().nextSibling().toElement().tagName() == "DataStructOp" || dataElement.firstChild().nextSibling().toElement().tagName() == "DataArrayOp")
        {
            if(dataElement.firstChild().nextSibling().toElement().tagName() == "DataStructOp")
                array.arrayType = "<structure>";

            if(dataElement.firstChild().nextSibling().toElement().tagName() == "DataArrayOp")
                array.arrayType = "array";

            for(int i = 0; i < valueList.size(); ++i)
            {
                QDomNode value = valueList.at(i);
                QDomElement valueElement = value.toElement();
                if(valueElement.tagName()=="DataStructOp"||valueElement.tagName()=="DataArrayOp")
                    parseDataArray(valueElement,array);
            }
        }

        data.array = array;
    }

}

void ConstantImport::parseConstValue(QDomElement &valueElement, ConstantDefine::DimensionArray &data)
{
    // 解析数组元素
    if(valueElement.hasAttribute("value"))
    {
        QString value = valueElement.attribute("value");
        if(suffix.contains(data.arrayType))
        {
            valueElement.setAttribute("value",value+suffix.value(data.arrayType));
        }
        data.elementValue.append(value);
    }
}

void ConstantImport::parseDataArray(const QDomElement &valueElement, ConstantDefine::DimensionArray &data)
{
    if(!valueElement.hasChildNodes())
        return;
    QDomElement dataElement = valueElement.firstChild().toElement();

    if(!dataElement.hasChildNodes())
        return;

    QDomElement nodeElement = dataElement.firstChild().toElement();

    QString valueTagName = nodeElement.tagName();

    QDomNodeList valueList = dataElement.childNodes();

    // 解析数组中数组
    if(valueElement.toElement().tagName() == "DataArrayOp")
    {
        ConstantDefine::DimensionArray array;
        if(valueTagName!="ConstValue")
            parseDataArray(valueElement,array);
        else{
            if(nodeElement.hasAttribute("value"))
            {
                QString value = nodeElement.attribute("value");

            }
            for(int i = 0; i < valueList.size(); ++i)
            {
                QDomNode value = valueList.at(i);
                QDomElement valueElement = value.toElement();
                parseConstValue(valueElement,array);
            }
        }

        data.array.append(array);
    }

    // 解析数组普通元素
//    if(valueTagName == "ConstValue")
//    {

//        if(nodeElement.hasAttribute("value"))
//        {
//            QString value = nodeElement.attribute("value");
//            data.arrayType = jugementBaseType(value);
//        }
//        for(int i = 0; i < valueList.size(); ++i)
//        {
//            QDomNode value = valueList.at(i);
//            QDomElement valueElement = value.toElement();
//            parseConstValue(valueElement,data);
//        }
//    }

    // 解析数组结构体
    if(valueElement.toElement().tagName() == "DataStructOp")
    {
        ConstantDefine::StructEleList structEleList;
        parseDataStructOp(valueElement,structEleList);
        data.structure.append(structEleList);
    }


}

void ConstantImport::saveConstants(QDomElement &element, const ConstantDefine::Constant &data, QDomDocument &doc)
{
    // 保存常量数据
    QDomElement constant = doc.createElement("Constant");
    constant.setAttribute("name",data.name);
    saveNormalData(constant,data,doc);
    element.appendChild(constant);
}

void ConstantImport::saveNormalData(QDomElement &element, const ConstantDefine::Constant &data, QDomDocument &doc)
{
    // 保存共有数据
    QDomElement type = doc.createElement("type");
    QDomElement NamedType = doc.createElement("NamedType");
    QDomElement typeIn = doc.createElement("type");
    QDomElement TypeRef = doc.createElement("TypeRef");
    TypeRef.setAttribute("name",data.typeName);

    typeIn.appendChild(TypeRef);
    NamedType.appendChild(typeIn);
    type.appendChild(NamedType);

    QDomElement value = doc.createElement("value");
    saveValueData(value,data,doc);

    QDomElement oid = doc.createElement("oid");
    oid.setAttribute("value",data.oid);

    element.appendChild(type);
    element.appendChild(value);
    element.appendChild(oid);

    if(!data.comments.isEmpty())
    {
        QDomElement pragmas = doc.createElement("pragmas");
        QDomElement comment = doc.createElement("comment");
        comment.setAttribute("value",data.comments);
        pragmas.appendChild(comment);
        element.appendChild(pragmas);
    }
}

void ConstantImport::saveValueData(QDomElement &element, const ConstantDefine::Constant &data, QDomDocument &doc)
{
    // 保存值数据
    QString baseType = data.baseType;

    // 数组类型
    if(baseType == "array")
    {
        saveDataArray(element,data.array,doc);
    }

    // 结构体类型
    else if(baseType == "<structure>")
    {
        saveDataStruct(element,data.structElements,doc);
    }

    // 普通类型
    else
    {
        saveConstValue(element,data.value,doc);
    }
}

void ConstantImport::saveConstValue(QDomElement &element, const QString &data, QDomDocument &doc)
{
    // 保存普通数据
    QDomElement constValue = doc.createElement("ConstValue");
    constValue.setAttribute("value",data);
    element.appendChild(constValue);
}

void ConstantImport::saveDataStruct(QDomElement &element, const ConstantDefine::StructEleList &datas, QDomDocument &doc)
{
    // 保存结构体数据
    QDomElement dataStructOp = doc.createElement("DataStructOp");
    QDomElement dataElement = doc.createElement("data");

    for(int i = 0; i < datas.size(); ++i)
    {
        ConstantDefine::StructEle data = datas.at(i);
        saveDataStructEle(dataElement,data,doc);
    }

    dataStructOp.appendChild(dataElement);
    element.appendChild(dataStructOp);
}

void ConstantImport::saveDataStructEle(QDomElement &element, const ConstantDefine::StructEle &data, QDomDocument &doc)
{
    // 保存结构体元素数据
    QString baseType = data.elementDef;

    QDomElement labelledExpression = doc.createElement("LabelledExpression");
    labelledExpression.setAttribute("label",data.elementName);
    QDomElement flow = doc.createElement("flow");

    // 结构体元素为数组
    if(baseType == "array")
    {
        saveDataArray(flow,data.array,doc);
    }

    // 结构体元素为结构体
    else if(baseType == "<structure>")
    {
        saveDataStruct(flow,data.structEles,doc);
    }

    // 结构体元素为普通类型
    else
    {
        saveConstValue(flow,data.elementValue,doc);
    }

    labelledExpression.appendChild(flow);
    element.appendChild(labelledExpression);
}

void ConstantImport::saveDataArray(QDomElement &element, const ConstantDefine::DimensionArray &data, QDomDocument &doc)
{
    // 保存数组数据
    QDomElement dataArrayOp = doc.createElement("DataArrayOp");
    QDomElement dataElement = doc.createElement("data");

    QString baseType = data.arrayType;

    // 数组元素为数组
    if(baseType == "array")
    {
        QList<ConstantDefine::DimensionArray> arrays = data.array;
        for(int i = 0; i < arrays.size(); ++i)
        {
            ConstantDefine::DimensionArray array = arrays.at(i);
            saveDataArray(dataElement,array,doc);
        }
    }

    // 数组元素为结构体
    else if(baseType == "<structure>")
    {
        QList<ConstantDefine::StructEleList> structEleLists = data.structure;

        for(int i = 0; i < structEleLists.size(); ++i)
        {
            ConstantDefine::StructEleList StructEleLis = structEleLists.at(i);
            saveDataStruct(dataElement,StructEleLis,doc);
        }
    }

    // 数组元素为普通类型
    else
    {
        QStringList values = data.elementValue;

        for(int i = 0; i < values.size(); ++i)
        {
            QString value = values.at(i);
            saveConstValue(dataElement,value,doc);
        }
    }

    dataArrayOp.appendChild(dataElement);
    element.appendChild(dataArrayOp);
}

QString ConstantImport::getOid(const QDomNode &idNode)
{
    // 获取oid
    QString oid;
    QDomElement idElement = idNode.toElement();
    if(idElement.hasAttribute("oid"))
    {
        oid = idElement.attribute("oid");
    }
    return  oid;
}

QString ConstantImport::getComment(const QDomNode &commentNode)
{
    // 获取描述信息
    QString comment;
    QDomElement comtElement = commentNode.toElement();

    comment = comtElement.text();

    return comment;
}

void ConstantImport::typeConvert(const QHash<QString,QString> &hash, ConstantDefine::Constant &data)
{

    if(hash.contains(data.typeName))
    {
        data.typeName=hash.value(data.typeName);
    }

}

void ConstantImport::oidConvert(ConstantDefine::Constant &data)
{
    data.oid=QUuid::createUuid().toString(QUuid::WithoutBraces);
}
